package com.chagee.channel.api.bo.lark;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.annotation.JSONField;
import com.alibaba.fastjson.serializer.JSONSerializer;
import com.alibaba.fastjson.serializer.ObjectSerializer;
import java.io.Serializable;
import java.lang.reflect.Type;
import javax.validation.constraints.NotBlank;
import lombok.Data;
import lombok.experimental.Accessors;

/**
 * @author: chang.jiawei
 * @date: 2024/01/16
 */
@Data
@Accessors(chain = true)
public class ApproveApprovalBO implements Serializable {

    private static final long serialVersionUID = 4632577984877800840L;
    @JSONField(name = "approval_code")
    @NotBlank(message = "approvalCode不能为空")
    private String approvalCode;

    @JSONField(name = "instance_code")
    @NotBlank(message = "instanceCode不能为空")
    private String instanceCode;

    /**
     * 审批人user_id,
     */
    @JSONField(name = "user_id")
    @NotBlank(message = "userId不能为空")
    private String userId;

    /**
     * 任务task_id,
     */
    @JSONField(name = "task_id")
    @NotBlank(message = "taskId不能为空")
    private String taskId;

    /**
     * 表单
     * 组件id请根据开放平台接口手动获取,记录在源项目中
     * https://open.feishu.cn/document/server-docs/approval-v4/approval/get
     * 控件值，详情见：
     * https://open.feishu.cn/document/server-docs/approval-v4/instance/create
     */
    @JSONField(serializeUsing = JSONArraySerializer.class)
    private JSONArray form;


    public static class JSONArraySerializer implements ObjectSerializer {

        @Override
        public void write(JSONSerializer serializer, Object object, Object fieldName, Type fieldType, int features) {

            JSONArray json = (JSONArray) object;
            serializer.write(json.toJSONString());
        }
    }
}
